package chen.web.back.client.system;

import chen.web.back.client.FormDataBuilder;
import chen.web.back.client.UrlHelper;

import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.Status;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.TextArea;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.Response;
import com.google.gwt.json.client.JSONParser;
import com.google.gwt.json.client.JSONValue;
import com.google.gwt.user.client.Element;

public class Log4jForm extends LayoutContainer {
	
	private TextArea propertiesArea;
	private String properties;
	private Button saveButton;
	private Status status = null;
	
	@Override
	protected void onRender(Element parent, int index) {
		// TODO Auto-generated method stub
		super.onRender(parent, index);
		VerticalPanel vp = new VerticalPanel();
		vp.add(createForm());
		add(vp);
		loadData();
	}
	
	
	private FormPanel createForm(){
		FormPanel simple = new FormPanel();
		simple.setHeading("log4j配置");
		simple.setFrame(true);
		simple.setWidth(665);
		simple.setHeight(640);
		simple.setLayout(new com.extjs.gxt.ui.client.widget.layout.AbsoluteLayout());
		
		propertiesArea = new TextArea();
		propertiesArea.setWidth(650);
		propertiesArea.setHeight(550);
		simple.add(propertiesArea);
		
		status = new Status();
		status.setBusy("please wait...");
		status.hide();
		status.setAutoWidth(true);
		simple.getButtonBar().add(status);
		
		saveButton = new Button("保存并生效");
		saveButton.addSelectionListener(new SelectionListener<ButtonEvent>() {
			public void componentSelected(ButtonEvent ce) {
				saveData();
			}
		});
		saveButton.disable();
		simple.addButton(saveButton);
		
		Listener<BaseEvent> inputListener = new Listener<BaseEvent>(){
			@Override
			public void handleEvent(BaseEvent be) {
				// TODO Auto-generated method stub
				if(!propertiesArea.getValue().equals(properties)
				){
					saveButton.enable();
				}
			}
			
		};
		propertiesArea.addListener(Events.OnChange, inputListener);
		propertiesArea.addListener(Events.OnKeyPress, inputListener);
		return simple;
	}
	
	private void saveData(){
		status.show();
		saveButton.disable();
		RequestBuilder builder = new RequestBuilder(RequestBuilder.POST, UrlHelper.toAbsolute("/log4j"));
		builder.setHeader("Content-Type", "application/x-www-form-urlencoded");
		try {
			FormDataBuilder fdb = new FormDataBuilder();
			fdb.addProperty("properties", propertiesArea.getValue());
			fdb.addProperty("_method", "PUT");
			builder.sendRequest(fdb.toQueryString(), new RequestCallback(){

				@Override
				public void onError(Request arg0, Throwable arg1) {
					// TODO Auto-generated method stub
					displayError("Couldn't retrieve JSON");
				}

				@Override
				public void onResponseReceived(Request request, Response response) {
					// TODO Auto-generated method stub
					if (200 == response.getStatusCode()) {
						JSONValue json = JSONParser.parseStrict(response
								.getText());
						boolean success = json.isObject().get("success").isBoolean().booleanValue();
						if(!success){
							displayError(response.getText());
						} 
						status.hide();
					}  else {
						displayError("Couldn't retrieve JSON ("
								+ response.getStatusText() + ")");
					}
				}
				
			});
		} catch (RequestException e) {
			// TODO Auto-generated catch block
			displayError("Couldn't retrieve JSON");
		}
	}
	
	private void loadData(){
		status.show();
		saveButton.disable();
		RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, UrlHelper.toAbsolute("/log4j"));
		try {
			builder.sendRequest(null, new RequestCallback(){

				@Override
				public void onError(Request arg0, Throwable arg1) {
					// TODO Auto-generated method stub
					displayError("Couldn't retrieve JSON");
				}

				@Override
				public void onResponseReceived(Request request, Response response) {
					// TODO Auto-generated method stub
					if (200 == response.getStatusCode()) {
						JSONValue json = JSONParser.parseStrict(response
								.getText());
						properties = json.isObject().get("returnObject").isString().stringValue();
						propertiesArea.setValue(properties);
						status.hide();
					}  else {
						displayError("Couldn't retrieve JSON ("
								+ response.getStatusText() + ")");
					}
				}
				
			});
		} catch (RequestException e) {
			// TODO Auto-generated catch block
			displayError("Couldn't retrieve JSON");
		}
	}
	
	private void displayError(String message){
		status.hide();
		saveButton.enable();
		MessageBox.alert("出错", message, null);
	}
}
